package cn.south.campus.admin.sys.service.impl;

import cn.south.campus.admin.sys.mapper.SysNoticeMapper;
import cn.south.campus.admin.sys.model.entity.SysNotice;
import cn.south.campus.admin.sys.model.req.SysNoticeReq;
import cn.south.campus.admin.sys.model.vo.SysNoticeVo;
import cn.south.campus.admin.sys.service.SysNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;


/**
 * SysNotice 实现层
 *
 * @author chow
 * @since 2024-11-06
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    @Override
    public void add(SysNoticeVo sysNoticeVo) {
        SysNotice sysNotice = SysNoticeVo.convert(sysNoticeVo);
        save(sysNotice);
    }

    @Override
    public IPage<SysNoticeVo> sysNoticePage(SysNoticeReq req) {
        IPage<SysNotice> page = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>()
                .like(StringUtils.isNotBlank(req.getNoticeTitle()), SysNotice::getNoticeTitle, req.getNoticeTitle())
                .like(StringUtils.isNotBlank(req.getNoticeContent()), SysNotice::getNoticeContent, req.getNoticeContent())
                .eq(Objects.nonNull(req.getNoticeType()), SysNotice::getNoticeType, req.getNoticeType())
                .eq(Objects.nonNull(req.getStatus()), SysNotice::getStatus, req.getStatus())
                .orderByDesc(SysNotice::getUpdateTime);
        return baseMapper.selectPage(page, wrapper).convert(SysNoticeVo::convert);
    }
}
